<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    design
 * @package     base_default
 * @copyright   Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
 * @license     http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
?>

<?php 
if( $this->getItems()->getSize() ): 
$count = count($this->getItems());
$page = ceil($count/3);
?>
<div id="myCarousel" class="block block-related carousel slide" data-interval="0">
	<div class="related-header">
		<div class="block-title">
			<strong>
				<span><?php echo $this->__('Related Products') ?></span>
			</strong>
		</div>
		<?php if($count > 3){?>
		<ul class="related-page carousel-indicators">
			<?php for( $i = 0; $i < $page; $i ++ ){?>
		    <li data-target="#myCarousel" data-slide-to="<?php echo $i;?>" class="<?php if ( $i == 0 ){ echo "active";}?>"></li>
		    <?php }?>
		</ul>
		<?php }?>
	</div>	
	<div class="left-product-base block-content">
		<div class="list-items">
			<div class="products-grid mini-products-list carousel-inner" id="block-related">
				<?php 
				$i = 0;
				foreach( $this->getItems() as $_item ): 
				$i ++;
				$now = date("Y-m-d");
				$newsFrom= substr($_item->getData('news_from_date'),0,10);
				$newsTo=  substr($_item->getData('news_to_date'),0,10);
				$specialprice = Mage::getModel('catalog/product')->load($_item->getId())->getSpecialPrice();
				$price = Mage::getModel('catalog/product')->load($_item->getId())->getPrice();
				$saleoff= round(($price - $specialprice)/$price*100) ;
				?>
				<?php if ( $i%3 == 1 ):?>
				<div class="item <?php if ( $i == 1 ){ echo "active";}?>">
				<?php endif;?>
					<div class="item-inner sub-item">
						<div class="product-image">
							<a href="<?php echo $_item->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_item->getName()) ?>">
								<img src="<?php echo $this->helper('catalog/image')->init($_item, 'thumbnail')->resize(84,84) ?>" alt="<?php echo $this->htmlEscape($_item->getName()) ?>" /> 
							</a>
						</div>				
					
						<div class="item-box-content">
							<div class="product-name">
								<a href="<?php echo $_item->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_item->getName(), null, true) ?>"> <?php echo $this->htmlEscape($_item->getName()) ?></a>
							</div>
							<div class="price-wrapper">
								<div class="price-block">
									<?php echo $this->getPriceHtml($_item, true) ?>
								</div>
							</div>
							
							<div class="item-review">
								<?php  echo $this->getReviewsSummaryHtml($_item, "short", true); ?>
							</div>	

						</div>	
					</div>
				<?php if ( $i%3 == 0 || $i == $count ):?>	
				</div>
				<?php 
				endif;
				endforeach; 
				?>
			</div>
			<script type="text/javascript">decorateList('block-related', 'none-recursive')</script>
		</div>
	</div>
	
<script type="text/javascript">
    //<![CDATA[
    $$('.related-checkbox').each(function(elem){
        Event.observe(elem, 'click', addRelatedToProduct)
    });

    var relatedProductsCheckFlag = false;
    function selectAllRelated(txt){
        if (relatedProductsCheckFlag == false) {
            $$('.related-checkbox').each(function(elem){
                elem.checked = true;
            });
            relatedProductsCheckFlag = true;
            txt.innerHTML="<?php echo $this->__('unselect all') ?>";
        } else {
            $$('.related-checkbox').each(function(elem){
                elem.checked = false;
            });
            relatedProductsCheckFlag = false;
            txt.innerHTML="<?php echo $this->__('select all') ?>";
        }
        addRelatedToProduct();
    }

    function addRelatedToProduct(){
        var checkboxes = $$('.related-checkbox');
        var values = [];
        for(var i=0;i<checkboxes.length;i++){
            if(checkboxes[i].checked) values.push(checkboxes[i].value);
        }
        if($('related-products-field')){
            $('related-products-field').value = values.join(',');
        }
    }
    //]]>
</script>
</div>
<?php endif ?>
